﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SRW.BLL;
using SRW;

public partial class ks1 : System.Web.UI.Page
{
    public string zhuanxiang = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TimeSs.Text = "60";
            TimeMm.Text = "39";
            LinkButton1.Enabled = false;

            int pkID = int.Parse(Request.QueryString["ID"].ToString());
            zhuanxiang = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
            //获取选择题试题
            RepListContent.DataSource = GetProList("选择题");
            RepListContent.DataBind();
            for (int i = 0; i < RepListContent.Items.Count; i++)
            {
                //序号绑定
                RepeaterItem reItem = (RepeaterItem)RepListContent.Items[i];
                Label LabSeNumber = (Label)reItem.FindControl("LabSeNumber");
                LabSeNumber.Text = Convert.ToString(i+1);
            }

            //获取判断题试题
            Repeater1.DataSource = GetProList("判断题");
            Repeater1.DataBind();
            for (int j = 0; j < Repeater1.Items.Count; j++)
            {
                //序号绑定
                RepeaterItem reItem = (RepeaterItem)Repeater1.Items[j];
                Label LabPanNumber = (Label)reItem.FindControl("LabPanNumber");
                LabPanNumber.Text = Convert.ToString(j + 1);
            }

            if (Session["username"] != null)
            {
                Page.Header.Title = "考生："+Session["username"].ToString()+"--浙江树人大学网上体育理论考试：" + zhuanxiang;
            }
        }
       
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            TimeMm.Text = "0";
            TimeSs.Text = "0";
            TimeMm.UpdateAfterCallBack = true;
            TimeSs.UpdateAfterCallBack = true;


            //时间到进行强制提交
            int TotalXz = 0;
            int TotalPd = 0;
            for (int i = 0; i < RepListContent.Items.Count; i++)
            {
                //计算选择题
                RepeaterItem reItem = (RepeaterItem)RepListContent.Items[i];
                RadioButtonList RBL_Content = (RadioButtonList)reItem.FindControl("RBL_Content");

                Label Label1 = (Label)reItem.FindControl("Label1");
                string str = "";
                if (RBL_Content.SelectedItem != null)
                {
                    str = RBL_Content.SelectedItem.Text;
                }

                string rightStr = SQL.GetData("select rightname from rightan where problemID=" + int.Parse(Label1.Text) + "", "rightname");

                if (str.Equals(rightStr))
                {
                    TotalXz = TotalXz + 2;
                }

            }

            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                //计算判断题
                RepeaterItem reItem = (RepeaterItem)Repeater1.Items[i];
                RadioButtonList RBL_Content1 = (RadioButtonList)reItem.FindControl("RBL_Content1");

                Label Label11 = (Label)reItem.FindControl("Label11");

                string str = "";

                if (RBL_Content1.SelectedItem != null)
                {
                    str = RBL_Content1.SelectedItem.Text;
                }
                string rightStr = SQL.GetData("select rightname from rightan where problemID=" + int.Parse(Label11.Text) + "", "rightname");

                if (str.Equals(rightStr))
                {
                    TotalPd = TotalPd + 1;
                }
            }

            Wtmark mWtmark = new Wtmark();
            int pkID = int.Parse(Request.QueryString["ID"].ToString());
            mWtmark.markm = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
            mWtmark.markrigfa = TotalPd;
            mWtmark.marksingse = TotalXz;
            mWtmark.papertest = Label6.Text;
            mWtmark.stunumber = Convert.ToString(Session["username"]);
            mWtmark.totalmark = mWtmark.markrigfa + mWtmark.marksingse;
            if (bWtmark.Insert(mWtmark, false))
            {
                Anthem.Manager.AddScriptForClientSideEval("alert('答题时间已到，现在可以查看你的成绩！')");
                Response.Redirect("stuks.aspx?Classid=4");
            }
            return;
        }
        TimeSs.Text = Convert.ToString(int.Parse(TimeSs.Text) - 1);
        if (int.Parse(TimeSs.Text) < 0)
        {
            //当小于0，重新由59递减，做成一个时钟的效果
            TimeMm.Text = Convert.ToString(int.Parse(TimeMm.Text) - 1);
            TimeSs.Text = "59";
            TimeMm.UpdateAfterCallBack = true;
        }
        TimeSs.UpdateAfterCallBack = true;
    }
    protected void RepListContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //绑定内层的数据
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label Label1 = (Label)e.Item.FindControl("Label1");

            RadioButtonList RBL_Content = (RadioButtonList)e.Item.FindControl("RBL_Content");

            setRBLConData(RBL_Content, Label1);

        }

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //绑定内层的数据
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label Label11 = (Label)e.Item.FindControl("Label11");

            RadioButtonList RBL_Content1 = (RadioButtonList)e.Item.FindControl("RBL_Content1");

            setRBLConData(RBL_Content1, Label11);

        }

    }
    //RadioButtonList数据绑定
    public void setRBLConData(RadioButtonList RBL,Label Lab)
    {
        RBL.DataTextField = "wtanname";
        RBL.DataValueField = "wtanname";
        RBL.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + int.Parse(Lab.Text) + "");
        RBL.DataBind();
    }
    /// <summary>
    /// 返回抽取的题目
    /// </summary>
    /// <param name="ProType"></param>
    /// <returns></returns>
    public List<Wtproblem> GetProList(string ProType)
    {
        int pkID = int.Parse(Request.QueryString["ID"].ToString());
        //删选试卷，再用Random随机数得到其中一份试卷
        string sinoname = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
        List<string> mList = bWtpaper.GetPaper("select distinct papername from wtpaper where sinoname='" + sinoname + "'", "papername");
        Random rnd = new Random();
        string papername = mList[rnd.Next(mList.Count)];
        Label6.Text = papername;
        List<string> mProIdList = bWtpaper.GetPaper("select distinct problemID from wtpaper where papername='" + papername + "'", "problemID");

        List<Wtproblem> results = new List<Wtproblem>();

        for (int i = 0; i < mProIdList.Count; i++)
        {
            Wtproblem mWtproblem = bWtproblem.select("select * from wtproblem where pkID=" + int.Parse(mProIdList[i].ToString()) + "");
            if (mWtproblem.wtprotype.Equals(ProType))
            {
                results.Add(mWtproblem);
            }
        }
        return results;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        //提交练习
        int TotalXz=0;
        int TotalPd = 0;
        for (int i = 0; i < RepListContent.Items.Count; i++)
        {
            //计算选择题
            RepeaterItem reItem = (RepeaterItem)RepListContent.Items[i];
            RadioButtonList RBL_Content = (RadioButtonList)reItem.FindControl("RBL_Content");

            Label Label1 = (Label)reItem.FindControl("Label1");
            string str = "";
            if (RBL_Content.SelectedItem != null)
            {
                 str = RBL_Content.SelectedItem.Text;
            }
            string rightStr = SQL.GetData("select rightname from rightan where problemID=" + int.Parse(Label1.Text) + "", "rightname");

            if (str.Equals(rightStr))
            {
                TotalXz = TotalXz + 2;
            }

        }

        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            //计算判断题
            RepeaterItem reItem = (RepeaterItem)Repeater1.Items[i];
            RadioButtonList RBL_Content1 = (RadioButtonList)reItem.FindControl("RBL_Content1");

            Label Label11 = (Label)reItem.FindControl("Label11");

            string str = "";

            if (RBL_Content1.SelectedItem != null)
            {
                str = RBL_Content1.SelectedItem.Text;
            }
            string rightStr = SQL.GetData("select rightname from rightan where problemID=" + int.Parse(Label11.Text) + "", "rightname");

            if (str.Equals(rightStr))
            {
                TotalPd = TotalPd + 1;
            }
        }

        Wtmark mWtmark = new Wtmark();
        int pkID = int.Parse(Request.QueryString["ID"].ToString());
        mWtmark.markm = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
        mWtmark.markrigfa = TotalPd;
        mWtmark.marksingse = TotalXz;
        mWtmark.papertest = Label6.Text;
        mWtmark.stunumber = Convert.ToString(Session["username"]);
        mWtmark.totalmark = mWtmark.markrigfa + mWtmark.marksingse;
        mWtmark.StudentName = SQL.GetData("select RealName from wtuser where wtusername = '" + mWtmark.stunumber + "'", "RealName");

        if (bWtmark.Insert(mWtmark, false))
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('你的考试成绩为：" + mWtmark.totalmark + "！现在可以查看！')");
            Response.Redirect("stuks.aspx?Classid=4");
        }
    }
}
